<template>
	<view class="main">
		<view class="header">
			<!-- <image class="avatar" :src="info.avatar" mode=""></image> -->
			<view class="touxiang">
				<u--image :showLoading="true" :src="seleUrl" width="80px" height="80px" shape="circle"
					@click="upload"></u--image>
			</view>
			<view class="info">
				<text v-if="info.is_partner">{{info.username}}
				【{{info.level_name}}】合伙人</text>
				<text v-else>{{info.username}}【{{info.level_name}}】</text>
				<text>{{info.nickname || '-'}}</text>
			</view>
<template v-if="info.rank == 1">
			<view class="switch" @tap="show = true">
				切换账号
			</view>
	</template>		
			<!-- <view class="switch">
				提货
			</view> -->
		</view>
		 <template v-if="info.rank == 1">
		<view class="fnOne">
			<view class="itemBox">
				<view class="item" @tap="routerTo('/pageC/hjf')">
					<text class="score">{{info.hasmoney}}</text>
					<text>红积分</text>
				</view>
				<view class="item" @tap="routerTo('/pageC/ljf')">
					<text class="score">{{info.hasscore}}</text>
					<text>绿积分</text>
				</view>
				<view class="item" @tap="routerTo('/pageC/xfjf')">
					<text class="score">{{info.hascp}}</text>
					<text>消费积分</text>
				</view>
				<view class="item" @tap="routerTo('/pageC/jkjf')">
					<text class="score">{{info.hasmoneys}}</text>
					<text>云积分</text>
				</view>
				<view class="item scores" @tap="routerTo('/pageC/trade')">
					<text>积分转账</text>
				</view>
			</view>
		</view>
		</template>
		
		<view class="fnTwo">
			<view class="tit">
				我的订单
			</view>
			<view class="itemBox">
				<view class="item" @tap="routerToTab('/pageD/orderList')">
					<image src="@/static/img/my2.png" mode=""></image>
					<text>全部订单</text>
				</view>
				<view class="item" @tap="routerToTab('/pageD/orderList')">
					<image src="@/static/img/my1.png" mode=""></image>
					<text>待发货</text>
				</view>
				<view class="item" @tap="routerToTab('/pageD/orderList')">
					<image src="@/static/img/my3.png" mode=""></image>
					<text>待收货</text>
				</view>
				<view class="item" @tap="routerToTab('/pageD/orderList')">
					<image src="@/static/img/my4.png" mode=""></image>
					<text>已完成</text>
				</view>
			</view>
		</view>
		<view class="fnTwo">
		  <view class="tit">
		    我的服务
		  </view>
		  <view class="itemBox">
		    <!-- rank为1时显示这些项目 -->
		    <template v-if="info.rank == 1">
		      <view class="item" @tap="routerTo('/pageC/businessCard')">
		        <image src="@/static/img/my9.png" mode=""></image>
		        <text>我的名片</text>
		      </view>
		      <view class="item" @tap="routerTo('/pageC/pointsMarket')">
		        <image src="@/static/img/points.png" mode=""></image>
		        <text>积分市场</text>
		      </view>
		      <view class="item" @tap="routerTo('/pageB/tuangou')">
		        <image src="@/static/img/my8.png" mode=""></image>
		        <text>我的团购</text>
		      </view>
		      <view class="item" @tap="routerTo('/pageC/tuangoushifang')">
		        <image src="@/static/img/my6.png" mode=""></image>
		        <text>团购释放</text>
		      </view>
		      <view class="item" @tap="routerTo('/pageC/hehuoren')">
		        <image src="@/static/img/my5.png" mode=""></image>
		        <text>合伙人释放</text>
		      </view>
		      <view class="item" @tap="routerTo('/pageC/recharge')">
		        <image src="@/static/img/my7.png" mode=""></image>
		        <text>充值管理</text>
		      </view>
		      <view class="item" @tap="routerTo('/pageC/delivery')">
		        <image src="@/static/img/my11.png" mode=""></image>
		        <text>提货管理</text>
		      </view>
		      <view class="item" @tap="routerTo('/pageB/myteam')">
		        <image src="@/static/img/my18.png" mode=""></image>
		        <text>我的团队</text>
		      </view>
		    </template>
		    
		    
		    <view class="item" @tap="routerTo('/pageB/address')">
		      <image src="@/static/img/my10.png" mode=""></image>
		      <text>收货地址</text>
		    </view>
		    <view class="item" @tap="routerTo('/pageB/setUp')">
		      <image src="@/static/img/my19.png" mode=""></image>
		      <text>设置</text>
		    </view>
		  </view>
		</view>

		<view class="bottomTxt">
			<text @tap="routerTo('/pageB/text?name=Agreement&title=用户协议')">《用户协议》</text>
			<text @tap="routerTo('/pageB/text?name=Privacy&title=隐私政策')">《隐私政策》</text>
		</view>
		<!-- 模态框 -->
		<u-popup round="15" :show="show" mode="bottom">
			<view class="popup">
				<u-icon class="close" name="close" color="#1D242C" size="28" @tap="show = false"></u-icon>
				<view class="tit" @tap="goToLogin()">
					切换账号
				</view>
				<view class="list">
					<view v-if="noson" class="no">
						{{noson}}
					</view>
					<u-radio-group v-else iconPlacement="right" placement="column" borderBottom @change="goToLogin">
						<u-radio v-for="(item,index) in sonList" :key="index" labelColor="#red" activeColor="#137854"
							:name="item.id" :label="item.username"></u-radio>
					</u-radio-group>
				</view>
			</view>
		</u-popup>

		<u-modal confirmColor="#fff" :show="show2" title="客服" @confirm="show2 = false" @close="show2 = false"
			:closeOnClickOverlay="true">
			<view class="slot-content" style="color: #fff;">
				{{msg2}}
			</view>
		</u-modal>
	</view>
</template>

<script>
	import {
		user,
		sons,
		sonLogin,
		getConfigs
	} from '@/api/index.js'
	export default {
		data() {
			return {
				seleUrl: '/static/img/tabbar/mine.png',
				info: {},
				show: false,
				sonList: [],
				noson: '',
				show2: false,
				msg2: ''
			}
		},
		onLoad() {
			getConfigs({
				name: 'customer'
			}).then(res => {
				this.msg2 = res.data.value
			})
		},
		onShow() {
			this.getInfo();
			const accountId = this.info.id;
			const cachedAvatarUrl = uni.getStorageSync('avatarUrl');
			if (cachedAvatarUrl) {
				this.seleUrl = cachedAvatarUrl;
			} else {
				this.seleUrl = '/static/img/tabbar/mine.png';
			}
		},
		methods: {
			goToLogin(e) {
				console.log(e)
				sonLogin({
					id: e
				}).then(res => {
					uni.setStorageSync('token', res.data.userinfo.token)
					this.show = false
					this.getInfo()
				})
			},
			async getInfo() {
				let res = await user()
				this.info = res.data
				sons().then(res => {
					console.log(res)
					if(res.code == 1){
						if(res.data.length == 0){
							this.noson = '暂无子账号'
						}else{
							this.noson = ''
						}
						this.sonList = res.data
					}else{
						this.noson = '暂无子账号'
					}
				})
				},
			upload() {
				uni.chooseImage({
					count: 1,
					sourceType: ['album'],
					success: (res) => {
						console.log(res);
						this.uploadFilePromise(res.tempFilePaths[0]);
					}
				});
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://www.zpyd.top/api/common/upload',
						filePath: url,
						name: 'file',
						header: {
							'token': uni.getStorageSync('token')
						},
						formData: {
							user: 'test'
						},
						success: (res) => {
							let ndata = JSON.parse(res.data);
							                this.seleUrl = ndata.data.fullurl;
							                uni.setStorageSync('avatarUrl', ndata.data.fullurl); // 存储头像 URL
							                console.log('头像已存储到缓存:', ndata.data.fullurl); // 打印存储的头像 URL
							                uni.showToast({
							                    title: '上传成功',
							                    icon: 'none'
							                });
						}
					});
				});
			},
			// goToLogin() {
			// 	this.show = false;
			// 	uni.reLaunch({
			// 		url: '/pages/login/login'
			// 	});
			// },
			routerTo(url) {
				uni.navigateTo({
					url: url
				})
			},
			routerToTab(url) {
				uni.navigateTo({
					url: url
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		min-height: 100vh;
		background-color: #f6f6f6;
		padding: 140rpx 30rpx;

		::v-deep.u-popup__content {
			background-color: #fff;
		}

		.popup {
			padding: 60rpx;
			position: relative;

			.tit {
				text-align: center;
				margin-bottom: 40rpx;
				color: #3c3c3c;
			}

			::v-deep.u-radio {
				margin-bottom: 20rpx;
			}

			.close {
				position: absolute;
				top: 40rpx;
				right: 40rpx;
			}

			.no {
				text-align: center;
				color: #3c3c3c;
			}
		}

		.header {
			display: flex;
			align-items: center;
			height: 260rpx;
			padding: 0 24rpx;
			background: linear-gradient(178deg, #EA504F 0%, #EA5B5A 47%, #EA7372 71%, #EA8281 87%, #EA8887 100%);
			box-sizing: border-box;
			border-radius: 30rpx 30rpx 0 0;

			.avatar {
				width: 136rpx;
				height: 136rpx;
				border-radius: 2000rpx;
			}

			.info {
				display: flex;
				flex-direction: column;
				height: 136rpx;
				justify-content: space-evenly;
				color: #fff;
				flex: 1;
				margin: 0 30rpx;


			}

			.switch {
				padding: 20rpx 30rpx;
				border: 4rpx solid #fff;
				color: #fff;
				border-radius: 200rpx;
			}
		}

		.fnOne {
			padding: 32rpx 12rpx;
			background-color: #fff;
			border-radius: 0 0 30rpx 30rpx;
			margin-bottom: 32rpx;

			.tit {
				font-weight: 700;
				font-size: 32rpx;
				color: #3c3c3c;
				margin-bottom: 40rpx;
			}

			.itemBox {
				display: flex;
				justify-content: space-around;
				align-items: center;

				.item {
					display: flex;
					flex-direction: column;
					align-items: center;

					image {
						width: 64rpx;
						height: 64rpx;
						margin-bottom: 16rpx;
					}

					.score {
						display: block;
						margin-bottom: 16rpx;
					}

					text {
						font-size: 24rpx;
						color: #3c3c3c;
					}
				}

				.scores {
					display: block;
					border: 1rpx solid #ccc;
					padding: 3rpx 12rpx;
					border-radius: 8rpx;
					box-sizing: border-box;
					box-sizing: border-box;
				}
			}
		}

		.touxiang {}



		.fnTwo {
			padding: 32rpx 32rpx 0;
			background-color: #fff;
			border-radius: 30rpx;
			margin-bottom: 32rpx;
			box-sizing: border-box;

			.tit {
				font-weight: 700;
				font-size: 32rpx;
				color: #3c3c3c;
				margin-bottom: 40rpx;
			}

			.itemBox {

				display: grid;
				grid-template-columns: repeat(4, 1fr);

				.item {

					display: flex;
					flex-direction: column;
					align-items: center;
					margin-bottom: 40rpx;

					image {
						width: 64rpx;
						height: 64rpx;
						margin-bottom: 16rpx;
					}

					text {
						font-size: 28rpx;
						color: #3c3c3c;
					}
				}
			}
		}

		.bottomTxt {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-top: 40rpx;

			text {
				color: #ff6347;
				font-size: 28rpx;
				margin-bottom: 10rpx;
			}
		}
	}
</style>